package com.example.eldersystem.operatedata.service;

import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.mapper.SystemUserMapper;
import com.example.eldersystem.operatedata.model.SystemUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemUserService {

    private SystemUserMapper systemUserMapper;

    @Autowired
    public void setSystemUserMapper (SystemUserMapper systemUserMapper) {
        this.systemUserMapper = systemUserMapper;
    }

    public int getSystemUserNum(){
        return systemUserMapper.selectSystemUserNum();
    }

    public DataResponse<String> addSystemUser(SystemUser systemUser) {
        DataResponse<String> dataResponse = new DataResponse<>();
        if(systemUserMapper.selectSystemUserByUserName(systemUser.getUserName())>=1){
            dataResponse.setCode(21);
            dataResponse.setMsg("该用户名已被注册");
        }else if(systemUserMapper.selectSystemUserByUserName(systemUser.getUserName())==0){
            if (systemUserMapper.addSystemUser(systemUser)>0){
                dataResponse.setCode(0);
                dataResponse.setMsg("注册成功");
            }else{
                dataResponse.setCode(22);
                dataResponse.setMsg("注册失败");
            }
        }
        return dataResponse;
    }


    public DataResponse<List<SystemUser>> selectSystemUser(int from, int num){
        DataResponse<List<SystemUser>> dataResponse = new DataResponse<>();
        List<SystemUser> list;
        int total = systemUserMapper.selectSystemUserNum();
        if(total<=0){
            dataResponse.setCode(10);
            dataResponse.setMsg("查询无结果");
        }else{
            dataResponse.setMsg("查询成功");
            list = systemUserMapper.selectSystemUser(from,num);
            dataResponse.setData(list);
        }
        return dataResponse;
    }

    public DataResponse<List<SystemUser>> selectSystemUserByRealName(String realName){
        DataResponse<List<SystemUser>> dataResponse = new DataResponse<>();
        List<SystemUser> list;
        int total = systemUserMapper.selectSystemUserNum2(realName);
        if(total<=0){
            dataResponse.setCode(10);
            dataResponse.setMsg("查询无结果");
        }else{
            dataResponse.setMsg("查询成功");
            list = systemUserMapper.selectSystemUserByRealName(realName);
            dataResponse.setData(list);
        }
        return dataResponse;
    }

    public DataResponse<String> updateSystemUser(SystemUser systemUser){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = systemUserMapper.updateSystemUser(systemUser);
        dataResponse.setCode(0);
        if(num==0){
            dataResponse.setCode(31);
            dataResponse.setMsg("更新失败，查询不到对应的管理员");
        }else{
            dataResponse.setMsg("更新成功");
        }
        return dataResponse;
    }

    public DataResponse<String> updateLogoImage(int id,String logoImage){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = systemUserMapper.updateLogoImage(id,logoImage);
        dataResponse.setCode(0);
        if(num==0){
            dataResponse.setCode(31);
            dataResponse.setMsg("更新失败，查询不到对应的管理员");
        }else{
            dataResponse.setMsg("更新头像成功");
        }
        return dataResponse;
    }

    public DataResponse<String> deleteSystemUser(int id){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = systemUserMapper.deleteSystemUser(id);
        if(num==0){
            dataResponse.setCode(41);
            dataResponse.setMsg("删除失败");
        }else{
            dataResponse.setMsg("删除成功");
        }
        return dataResponse;
    }

    public DataResponse<SystemUser> login(String userName,String password){
        DataResponse<SystemUser> dataResponse = new DataResponse<>();
        SystemUser systemUser = new SystemUser();
        int id=systemUserMapper.login(userName,password);
        if(id>0){
            dataResponse.setMsg("登录成功,欢迎"+userName);
            systemUser = systemUserMapper.getCurrentUser(id);
            dataResponse.setData(systemUser);
        }else{
            dataResponse.setCode(11);
            dataResponse.setMsg("用户名或密码错误");
        }
        return dataResponse;
    }
}
